package com.wxy.demo.controller;

import cn.hutool.core.io.FileUtil;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;

/**
 * <p>
 * 返回值为空测试
 * </p>
 *
 * @author e-Xiaoyuan.Wang
 * @since 2023/1/6 16:42
 */
@RestController
@RequestMapping("null")
public class NullController {

    @GetMapping("demo1")
    public void demo1() {

    }

    @GetMapping("demo2")
    public void demo2() {

    }

    @GetMapping("demo3")
    public void demo2(HttpServletResponse response) throws IOException {
        File file = new File("doc/a.xml");
        //第一步：设置响应类型
        response.setContentType("application/force-download");//应用程序强制下载
        //第二步：设置响应头，对文件进行url编码
        String fileName = URLEncoder.encode(file.getName(), StandardCharsets.UTF_8);
        response.setHeader("Content-Disposition", "attachment;filename=" + fileName);
        FileUtil.writeToStream(file, response.getOutputStream());
    }
}
